package com.blog.service.impl;

import com.blog.entity.article.ArticleType;
import com.blog.mapper.ArticleMapper;
import com.blog.service.ArticleTypeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleTypeServiceImpl implements ArticleTypeService {

    @Resource
    private ArticleMapper mapper;

    @Override
    public List<ArticleType> getAllArticleTypes() {
        return mapper.getArticleTypes();
    }

    @Override
    public ArticleType getArticleTypeByTid(Integer tid) {
        return mapper.getArticleTypeByTid(tid);
    }

    @Override
    public String addArticleType(String type) {
        List<ArticleType> types = mapper.getArticleTypes();
        for (ArticleType t : types) {
            if (t.getType().equals(type)) {
                return "添加类型失败,请检查类型是否已存在";
            }
        }
        return mapper.addArticleType(type) != 0 ? null : "添加类型失败,请稍后再试";
    }

    @Override
    public String updateArticleType(ArticleType articleType) {
        List<ArticleType> types = mapper.getArticleTypes();
        for (ArticleType t : types) {
            if (t.getType().equals(articleType.getType())) {
                return "修改类型失败,请检查类型是否已存在";
            }
        }
        return mapper.updateArticleType(articleType) > 0 ? null : "修改类型失败,请稍后再试";
    }

    @Override
    public String deleteArticleType(Integer tid) {
        return mapper.deleteArticleTypeByTid(tid) > 0 ? null : "删除类型失败,请稍后再试";
    }
}
